package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCIn {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection()){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入账号: ");
            String username = sc.nextLine();
            System.out.println("请输入密码: ");
            String password = sc.nextLine();

            //预编译SQL语句select
            String sql = "select nickname from userinfo where username=? and password=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            //通过ps对象设置参数
            ps.setString(1, username);
            ps.setString(2, password);
            //执行SQL语句
            ResultSet r = ps.executeQuery();
            if(r.next()) {
                String str = r.getString("nickname");
                System.out.println("登录成功,欢迎回来: "+str);
            }else System.out.println("登录失败");
        } catch (SQLException e) {
            e.getMessage();
        }
    }
}














